1 Imports System.Data.SqlClient
2 Public Class frmRegistration
3
4 Sub Reset()
5 txtContactNo.Text = ""
6 txtEmailID.Text = ""
7 txtName.Text = ""
8 txtPassword.Text = ""
9 txtUserID.Text = ""
10 cmbUserType.SelectedIndex = -1
11 txtUserID.Focus()
12 btnSave.Enabled = True
13 btnUpdate.Enabled = False
14 btnDelete.Enabled = False
15 End Sub
16 Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
17 Me.Close()
18 End Sub
19
20 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
21 If txtUserID.Text = "" Then
22 MessageBox.Show("Please enter user id", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
23 txtUserID.Focus()
24 Return
25 End If
26 If cmbUserType.Text = "" Then
27 MessageBox.Show("Please select user type", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
28 cmbUserType.Focus()
29 Return
30 End If
31 If txtPassword.Text = "" Then
32 MessageBox.Show("Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
33 txtPassword.Focus()
34 Return
35 End If
36 If txtName.Text = "" Then
37 MessageBox.Show("Please enter name", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
38 txtName.Focus()
39 Return
40 End If
41 If txtContactNo.Text = "" Then
42 MessageBox.Show("Please enter contact no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
43 txtContactNo.Focus()
44 Return
45 End If
46
47 Try
48 con = New SqlConnection(cs)
49 con.Open()
50 Dim ct As String = "select userid from registration where userid='" & txtUserID.Text & "'"
51
52 cmd = New SqlCommand(ct)
53 cmd.Connection = con
54 rdr = cmd.ExecuteReader()
55
56 If rdr.Read() Then
57 MessageBox.Show("user id Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
58 txtUserID.Text = ""
59 txtUserID.Focus()
60 If (rdr IsNot Nothing) Then
61 rdr.Close()
62 End If
63 Return
64 End If
65
66 con = New SqlConnection(cs)
67 con.Open()
68
69 Dim cb As String = "insert into Registration(userid, UserType, Password, Name, ContactNo, EmailID,JoiningDate) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7)"
70 cmd = New SqlCommand(cb)
71 cmd.Connection = con
72 cmd.Parameters.AddWithValue("@d1", txtUserID.Text)
73 cmd.Parameters.AddWithValue("@d2", cmbUserType.Text)
74 cmd.Parameters.AddWithValue("@d3", Encrypt(txtPassword.Text.Trim()))
75 cmd.Parameters.AddWithValue("@d4", txtName.Text)
76 cmd.Parameters.AddWithValue("@d5", txtContactNo.Text)
77 cmd.Parameters.AddWithValue("@d6", txtEmailID.Text)
78 cmd.Parameters.AddWithValue("@d7", Now)
79 cmd.ExecuteReader()
80 con.Close()
81 con = New SqlConnection(cs)
82 con.Open()
83 Dim st As String = "added the new user '" & txtUserID.Text & "'"
84 Dim cb1 As String = "insert into Logs(userid,Operation,Date) VALUES (@d1,@d2,@d3)"
85 cmd = New SqlCommand(cb1)
86 cmd.Connection = con
87 cmd.Parameters.AddWithValue("@d1", lblUser.Text)
88 cmd.Parameters.AddWithValue("@d2", st)
89 cmd.Parameters.AddWithValue("@d3", System.DateTime.Now)
90 cmd.ExecuteReader()
91 con.Close()
92 MessageBox.Show("Successfully Registered", "User", MessageBoxButtons.OK, MessageBoxIcon.Information)
93 btnSave.Enabled = False
94 Getdata()
95 Catch ex As Exception
96 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
97 End Try
98 End Sub
99
100 Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
101 Try
102 If MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
103 DeleteRecord()
104 End If
105 Catch ex As Exception
106 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
107 End Try
108 End Sub
109 Private Sub DeleteRecord()
110
111 Try
112 If txtUserID.Text = "admin" Or txtUserID.Text = "Admin" Then
113 MessageBox.Show("Admin account can not be deleted", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
114 Exit Sub
115 End If
116 Dim RowsAffected As Integer = 0
117 con = New SqlConnection(cs)
118 con.Open()
119 Dim cq As String = "delete from Registration where userid='" & txtUserID.Text & "'"
120 cmd = New SqlCommand(cq)
121 cmd.Connection = con
122 RowsAffected = cmd.ExecuteNonQuery()
123 If RowsAffected > 0 Then
124 con = New SqlConnection(cs)
125 con.Open()
126 Dim st As String = "deleted the user '" & txtUserID.Text & "'"
127 Dim cb1 As String = "insert into Logs(userid,Operation,Date) VALUES (@d1,@d2,@d3)"
128 cmd = New SqlCommand(cb1)
129 cmd.Connection = con
130 cmd.Parameters.AddWithValue("@d1", lblUser.Text)
131 cmd.Parameters.AddWithValue("@d2", st)
132 cmd.Parameters.AddWithValue("@d3", System.DateTime.Now)
133 cmd.ExecuteReader()
134 con.Close()
135 MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
136 Getdata()
137 Reset()
138 Else
139 MessageBox.Show("No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information)
140 Reset()
141 End If
142 If con.State = ConnectionState.Open Then
143 con.Close()
144
145 End If
146 Catch ex As Exception
147 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
148 End Try
149 End Sub
150
151 Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
152 Try
153 If txtUserID.Text = "" Then
154 MessageBox.Show("Please enter user id", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
155 txtUserID.Focus()
156 Return
157 End If
158 If cmbUserType.Text = "" Then
159 MessageBox.Show("Please select user type", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
160 cmbUserType.Focus()
161 Return
162 End If
163 If txtPassword.Text = "" Then
164 MessageBox.Show("Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
165 txtPassword.Focus()
166 Return
167 End If
168 If txtName.Text = "" Then
169 MessageBox.Show("Please enter name", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
170 txtName.Focus()
171 Return
172 End If
173 If txtContactNo.Text = "" Then
174 MessageBox.Show("Please enter contact no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
175 txtContactNo.Focus()
176 Return
177 End If
178
179 con = New SqlConnection(cs)
180 con.Open()
181 Dim cb As String = "update registration set userid=@d1, usertype=@d2,password=@d3,name=@d4,contactno=@d5,emailid=@d6 where userid=@d7"
182 cmd = New SqlCommand(cb)
183 cmd.Connection = con
184 cmd.Parameters.AddWithValue("@d1", txtUserID.Text)
185 cmd.Parameters.AddWithValue("@d2", cmbUserType.Text)
186 cmd.Parameters.AddWithValue("@d3", Encrypt(txtPassword.Text.Trim()))
187 cmd.Parameters.AddWithValue("@d4", txtName.Text)
188 cmd.Parameters.AddWithValue("@d5", txtContactNo.Text)
189 cmd.Parameters.AddWithValue("@d6", txtEmailID.Text)
190 cmd.Parameters.AddWithValue("@d7", TextBox1.Text)
191 cmd.ExecuteReader()
192 con.Close()
193 con.Open()
194 Dim st As String = "updated the user '" & txtUserID.Text & "' details"
195 Dim cb1 As String = "insert into Logs(userid,Operation,Date) VALUES (@d1,@d2,@d3)"
196 cmd = New SqlCommand(cb1)
197 cmd.Connection = con
198 cmd.Parameters.AddWithValue("@d1", lblUser.Text)
199 cmd.Parameters.AddWithValue("@d2", st)
200 cmd.Parameters.AddWithValue("@d3", System.DateTime.Now)
201 cmd.ExecuteReader()
202 con.Close()
203 MessageBox.Show("Successfully updated", "User Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
204 btnUpdate.Enabled = False
205 Getdata()
206 Catch ex As Exception
207 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
208 End Try
209 End Sub
210 Public Sub Getdata()
211 Try
212 con = New SqlConnection(cs)
213 con.Open()
214 cmd = New SqlCommand("SELECT RTRIM(userid), RTRIM(UserType), RTRIM(Password), RTRIM(Name), RTRIM(EmailID), RTRIM(ContactNo),RTRIM(JoiningDate) from Registration order by JoiningDate", con)
215 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
216 dgw.Rows.Clear()
217 While (rdr.Read() = True)
218 dgw.Rows.Add(rdr(0), rdr(1), Decrypt(rdr(2)), rdr(3), rdr(4), rdr(5), rdr(6))
219 End While
220 con.Close()
221 Catch ex As Exception
222 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
223 End Try
224 End Sub
225 Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
226 Reset()
227 End Sub
228
229
230 Private Sub dgw_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles dgw.RowPostPaint
231 Dim strRowNumber As String = (e.RowIndex + 1).ToString()
232 Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font)
233 If dgw.RowHeadersWidth < Convert.ToInt32((size.Width + 20)) Then
234 dgw.RowHeadersWidth = Convert.ToInt32((size.Width + 20))
235 End If
236 Dim b As Brush = SystemBrushes.ControlText
237 e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))
238
239 End Sub
240
241 Private Sub frmRegistration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
242 Getdata()
243 End Sub
244
245 Private Sub dgw_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgw.MouseClick
246 Try
247 Dim dr As DataGridViewRow = dgw.SelectedRows(0)
248 txtUserID.Text = dr.Cells(0).Value.ToString()
249 TextBox1.Text = dr.Cells(0).Value.ToString()
250 cmbUserType.Text = dr.Cells(1).Value.ToString()
251 txtPassword.Text = dr.Cells(2).Value.ToString()
252 txtName.Text = dr.Cells(3).Value.ToString()
253 txtContactNo.Text = dr.Cells(5).Value.ToString()
254 txtEmailID.Text = dr.Cells(4).Value.ToString()
255 btnUpdate.Enabled = True
256 btnDelete.Enabled = True
257 btnSave.Enabled = False
258 Catch ex As Exception
259 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
260 End Try
261 End Sub
262 End Class